<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆管理</span>
    <el-button @click="dialogVisible=true" type="primary" style="float:right;margin-top:13px;">新建车辆</el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="100px">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆品牌">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.brand"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆牌号">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.license"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆型号">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.model"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆识别码">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.code"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车辆排量">
            <el-select placeholder="请选择" v-model="addVehicleForm.displacement">
              <el-option label="1.6" value="1"></el-option>
              <el-option label="2.5" value="2"></el-option>
              <el-option label="4" value="3"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="车辆类型">
            <el-select placeholder="请选择" v-model="addVehicleForm.type">
              <el-option v-for="item in vehicleOptions" :label="item.label" :value="item.value"></el-option>
              <!--              <el-option label="轿车" value="10"></el-option>-->
              <!--              <el-option label="客车" value="20"></el-option>-->
              <!--              <el-option label="货车" value="30"></el-option>-->
              <!--              <el-option label="挂车" value="40"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="车身颜色">
            <el-select placeholder="请选择" v-model="addVehicleForm.color">
              <el-option v-for="i in colorOptions" :label="i.label" :value="i.value"></el-option>
              <!--              <el-option label="黑" value="10"></el-option>-->
              <!--              <el-option label="白" value="20"></el-option>-->
              <!--              <el-option label="蓝" value="30"></el-option>-->
              <!--              <el-option label="灰" value="40"></el-option>-->
              <!--              <el-option label="银" value="50"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="里程数">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.kilometers"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="addVehicleForm.buyTime"
                            @change="validateDate"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="上牌时间">
            <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="addVehicleForm.regTime"
                            @change="validateDate"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="购买价格">
            <el-input placeholder="请输入内容" v-model="addVehicleForm.price"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电池类型">
            <el-select placeholder="请选择" v-model="addVehicleForm.batteryType">
              <el-option v-for="item in batteryOptions" :label="item.label" :value="item.value"></el-option>
              <!--              <el-option label="铅酸蓄电池" value="10"></el-option>-->
              <!--              <el-option label="镍氢电池" value="20"></el-option>-->
              <!--              <el-option label="钠硫电池" value="30"></el-option>-->
              <!--              <el-option label="二次锂电池" value="40"></el-option>-->
              <!--              <el-option label="空气电池" value="50"></el-option>-->
              <!--              <el-option label="三元锂电池" value="60"></el-option>-->
              <!--              <el-option label="碱性燃料电池" value="70"></el-option>-->
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible=false">取消</el-button>
      <el-button type="primary" @click="addVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 车辆搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="车辆品牌">
        <el-input placeholder="请输入内容" v-model="searchForm.brand" style="width:220px;"
                  @keydown.enter="loadVehicleTable"></el-input>
      </el-form-item>
      <el-form-item label="车牌号" style="width:290px;">
        <el-input placeholder="请输入内容" v-model="searchForm.license" style="width:220px;"
                  @keydown.enter="loadVehicleTable"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadVehicleTable">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="vehicleTableData">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="brand" label="车辆品牌" align="center"/>
      <el-table-column prop="license" label="车牌号" align="center"/>
      <el-table-column prop="code" label="车辆识别码" align="center"/>
      <el-table-column prop="type" label="车辆类型" align="center" :formatter="vehicleTypeFormatter"/>
      <el-table-column prop="price" label="购买价格" align="center"/>
      <el-table-column prop="buyTime" label="购买时间" align="center"/>
      <el-table-column prop="regTime" label="上牌时间" align="center"/>
      <el-table-column prop="batteryType" label="电池类型" align="center"
                       :formatter="batteryTypeFormatter"/>
      <el-table-column prop="status" label="车辆状态" align="center"
                       :formatter="vehicleStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="updateVehicle(scope.row)">编辑</el-button>
          <el-button link type="primary" size="small" @click="deleteVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新增车辆');
//定义存储车辆搜索条件
const searchForm = ref({
  brand: '',
  license: ''
});
//定义数组来存储表格数据
const vehicleTableData = ref([]);
//定义车辆选项loadDictOptions
const vehicleOptions = ref([]);
//颜色选项
const colorOptions = ref([]);
//电池选项
const batteryOptions = ref([]);
//加载车辆表方法
const loadVehicleTable = () => {
  const data = qs.stringify(searchForm.value);
  console.log("loadVehicleTable呗调用了");
  axios.get(BASE_URL + '/v1/vehicle/select/?' + data).then(response => {
    if (response.data.code == 2000) {
      ElMessage.success(response.data.msg);
      // console.log(response.data.data);
      vehicleTableData.value = response.data.data;
      // console.log(vehicleTableData.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}
//进入页面立即加载
onMounted(() => {
  loadVehicleTable();
  loadDictOptions(vehicleOptions, 'vehicle_type');
  loadDictOptions(colorOptions, 'vehicle_color');
  loadDictOptions(batteryOptions, 'battery_type');
  // console.log(vehicleOptions.value);
})

//定义重置搜索的方法
const resetSearch = () => {
  searchForm.value = {}
  loadVehicleTable();
}
const addVehicleForm = ref({
  id: '',
  brand: '',
  license: '',
  code: '',
  model: '',
  displacement: '',
  type: '',
  kilometers: '',
  price: '',
  buyTime: '',
  regTime: '',
  batteryType: '',
  status: ''
})
//定义新增车辆方法
const addVehicle = () => {
  if (!addVehicleForm.value.license || addVehicleForm.value.license.trim() == '') {
    ElMessage.error('请输入车牌号!');
    return;
  }
  if (!addVehicleForm.value.code || addVehicleForm.value.code.trim() == '') {
    ElMessage.error('请输入车辆识别码!');
    return;
  }
  if (!addVehicleForm.value.buyTime) {
    ElMessage.error('必须选择购买时间!');
    return;
  }
  console.log(addVehicleForm.value);
  const data = qs.stringify(addVehicleForm.value);
  console.log(data);
  axios.post(BASE_URL + '/v1/vehicle/add', data).then(response => {
    if (response.data.code == 2000) {
      ElMessage.success(response.data.msg);
      dialogVisible.value = false;
      addVehicleForm.value = {};
      console.log('addVehicle方法 的loadVehicleTable被调用了')
      dialogTitle.value = '新增车辆';
      loadVehicleTable();
    } else {
      ElMessage.error('车牌号与车辆识别码不允许重复');
    }
  });

}
//关闭弹窗方法
const handleClose = () => {
  if (confirm('您确认要关闭本弹窗吗?')) {
    addVehicleForm.value = {};
    dialogVisible.value = false;
    loadVehicleTable;
  }
}
//定义修改时的方法
const updateVehicle = (row) => {
  dialogTitle.value = '修改车辆';
  addVehicleForm.value = row;
  console.log(addVehicleForm.value);
  dialogVisible.value = true;
  // const data = qs.stringify(addVehicleForm.value);
  // console.log(data);
  // axios.post(BAS_URL+'/v1/vehicle/add',data).then(response=>{
  //   if(response.data.code==2000){
  //     ElMessage.success(response.data.msg);
  //   }else {
  //     ElMessage.error('车牌号与车辆识别码不允许重复');
  //   }
  // })

}

const validateDate = () => {
  const buyTime = addVehicleForm.value.buyTime;
  const regTime = addVehicleForm.value.regTime;
  if (buyTime && regTime && new Date(buyTime) >= new Date(regTime)) {
    ElMessage.error('购买时间不能大于等于上牌时间');
    addVehicleForm.value.regTime = '';
  }
}
//定义删除方法
const deleteVehicle = (id) => {
  if (confirm('您确认要删除该车辆吗?')) {
    axios.post(BASE_URL + '/v1/vehicle/delete/' + id).then(response => {
      if (response.data.code == 2000) {
        ElMessage.success(response.data.msg);
        loadVehicleTable();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
const vehicleTypeFormatter = (row, column, cellValue, index) => {
  if (!vehicleOptions.value) {
    return cellValue;
  }
  for (let i of vehicleOptions.value) {
    if (cellValue == i.value) {
      return i.label;
    }
  }
}
const batteryTypeFormatter = (row, column, cellValue, index) => {
  if (!vehicleOptions.value) {
    return cellValue;
  }
  for (let i of batteryOptions.value) {
    if (cellValue == i.value) {
      return i.label;
    }
  }
}

//对车辆状态显示内容进行文字转换
const vehicleStatusFormatter = (row,column,cellValue,index)=>{
  if(cellValue == 1){
    cellValue = '空闲';
  }else if(cellValue == 2){
    cellValue = '占用';
  }
  return cellValue;
}
</script>
<style scoped>

</style>